<?php
//No direct acesss
defined('_JEXEC') or die();
jimport('joomla.application.component.model');

class SimplelistingModelAuthor extends JModel {
        var $_data = null;
	var $_total = null;
	var $_pagination = null;
	var $_table_prefix = null;


    function __construct()
    {
        parent::__construct();

        global $mainframe, $context;

        $this->_table_prefix = '#__simplelisting';
        $params =&$mainframe->getParams();

        $limit	= $mainframe->getUserStateFromRequest( $context.'limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);


    }

    function getData()
    {
        if (empty($this->_data))
        {
                $query = $this->_buildQuery();
                $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }

        return $this->_data;
    }

    function getTotal()
    {
        if (empty($this->_total))
        {
                $query = $this->_buildQuery();
                $this->_total = $this->_getListCount($query);
        }

        return $this->_total;
    }

    function _buildQuery()
    {
        $where = $this->_buildContentWhere();
        $orderby = $this->_buildContentOrderBy();

        $query = 'SELECT a.*,
        a.name as `username`, a.email as `email`,
        s.name as `listingname` , s.id as `sid`, s.date_created as `posted`, c.name as `catname`, t.name as `tidname`,
        cd.telephone as `tel`, cd.mobile `mobile`, cd.fax as `fax`, cd.country as `usercountry`, cd.misc `misc`
        FROM #__users AS a
        LEFT JOIN #__contact_details AS cd ON cd.user_id = a.id
        LEFT JOIN #__simplelisting AS s ON s.create_by = a.id
        LEFT JOIN #__simplelisting_categories AS c ON c.id = s.category_id
        LEFT JOIN #__simplelisting_types AS t ON t.id = s.type_id'.

        $where
        .$orderby;
        return $query;
    }

    function _buildContentWhere()
    {
      global $mainframe, $context;
      $cid = $this->getCid();
      $where = ' WHERE a.id='.$cid[0].' AND s.published=1 AND c.published=1 AND t.published=1';
        
        return $where;
    }

    function _buildContentOrderBy()
    {
        global $mainframe, $context;

        $orderby = ' ORDER BY a.name DESC';
        return $orderby;
    }

    function getAuthors($id)
    {
        $query = 'SELECT a.*,
        a.name as `username`, a.email as `email`, s.name as `listingname`,
        cd.telephone as `tel`, cd.mobile `mobile`, cd.fax as `fax`, cd.country as `usercountry`,cd.misc `misc`
        FROM #__users AS a
        LEFT JOIN #__contact_details AS cd ON cd.user_id = a.id
        LEFT JOIN #__simplelisting AS s ON s.create_by = a.id
        WHERE a.id = '.$id;
        $db = $this->getDBO();
        $db->setQuery($query);
        $author = $db->loadObject();

        if ($author === null)
        JError::raiseError (500, JText::_('LISTING_NOT_FOUND_MSG'));
        else
        return $author;

    }

    	function getCid()
	{
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		JArrayHelper::toInteger( $cid, array(0) );

		return $cid ;
	}

}
?>
